package com.lanou3g.code0426;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/4/26;
 * Otherwise, I don't know who created it either
 */

public class Main {
    /*
    JDBC 一共有4个 核心类
    1. DriverManager
        用来管理驱动的,
    2. Connection
    3. Statement
    4. ResultSet
     */
    public static void main(String[] args) {
        // 1. 装载驱动
        try {

            Class.forName("com.mysql.jdbc.Driver");

            // 2. 获得连接
            Connection conn = DriverManager
                    .getConnection(
                            "jdbc:mysql://localhost:3306/lan_ou?serverTimezone=UTC",
                            "root",
                            "123456");
            // 3. 获得执行SQL语句的对象
            Statement state = conn.createStatement();

            //4. 执行sql语句
            String sql = "SELECT * FROM stu";
            ResultSet resultSet = state.executeQuery(sql);
            // 用来执行 没有结果的SQL
            // 返回值 int  用来表示 这条sql 影响了多少行数据
//            int row = state.executeUpdate(sql);

            while (resultSet.next()){
                String id = resultSet.getString("sid");
                String name = resultSet.getString("sname");
                String gender = resultSet.getString("gender");
                int age = resultSet.getInt("age");
//                System.out.println(resultSet
//                        .getObject("sid"));
//                System.out.println(resultSet
//                        .getObject("sname"));
//                System.out.println(resultSet
//                        .getObject("gender"));
//                System.out.println(resultSet
//                        .getObject("age"));
            }



            //5. 关闭资源
            state.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
